package com.yulongtian.oneProblemEveryDay.month202302;

import java.util.HashMap;

/**
 * https://leetcode.cn/problems/alphabet-board-path/
 * 可
 *
 * @author yulongTian
 * @create 2023-02-12 9:51
 */
public class Test09 {
    public static void main(String[] args) {
        System.out.println(alphabetBoardPath("leet"));
    }

    public static String alphabetBoardPath(String target) {
        String[] board = {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"};
        HashMap<Character, int[]> map = new HashMap<>();
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length(); j++) {
                map.put(board[i].charAt(j), new int[]{i, j});
            }
        }
        String ans = "";
        int cur_i = 0;
        int cur_j = 0;
        for (int k = 0; k < target.length(); k++) {
            char tar = target.charAt(k);
            char now = board[cur_i].charAt(cur_j);
            int[] ints = map.get(tar);
            int tar_i = ints[0];
            int tar_j = ints[1];
            if (tar == now) {
                ans += "!";
                cur_i = tar_i;
                cur_j = tar_j;
                continue;
            }
            // 上左
            if (tar_i <= cur_i && tar_j <= cur_j) {
                for (int p = tar_i; p < cur_i; p++) {
                    ans += "U";
                }
                for (int p = tar_j; p < cur_j; p++) {
                    ans += "L";
                }
            } else if (tar_i <= cur_i) {// 上右
                for (int p = tar_i; p < cur_i; p++) {
                    ans += "U";
                }
                for (int p = cur_j; p < tar_j; p++) {
                    ans += "R";
                }
            } else if (tar_j <= cur_j) {// 下左
                for (int p = tar_j; p < cur_j; p++) {
                    ans += "L";
                }
                for (int p = cur_i; p < tar_i; p++) {
                    ans += "D";
                }
            } else {// 下右
                for (int p = cur_i; p < tar_i; p++) {
                    ans += "D";
                }
                for (int p = cur_j; p < tar_j; p++) {
                    ans += "R";
                }
            }
            ans += "!";
            cur_i = tar_i;
            cur_j = tar_j;
        }
        return ans;
    }

}
